#include <assert.h>

#include "button.h"

EXPORT widget_t *create_button(parent,text,x,y,width,height,extstyle)
    widget_t *parent;
    char *text;
    int32_t x,y;
    uint32_t width,height;
    uint32_t extstyle;
{
    // format button style
    uint32_t style = WS_VISIBLE|WS_CHILD|WS_TABSTOP;
    // allocate memory for the button
    widget_t *self = malloc(sizeof(widget_t));
    assert(self != NULL);
    // allocate a copy the text string
    self->text = malloc(strlen(text)+1);
    assert(self->text != NULL);
    strcpy(self->text, text);
    // create the window
    self->hwnd = CreateWindowEx(0, "BUTTON", text, style, x, y, width, height,
                    parent->hwnd, 0, GetModuleHandle(0), 0);
    // bind this window with the callback
    callback_bind(self->hwnd, self);
    // set values fo the width
    self->parent = parent;
    self->width = width;
    self->height = height;
    self->ext = NULL;
    self->memhdc = NULL;
    self->bmp = NULL;
    self->oldbmp = NULL;
    self->backbuf = NULL;
    // return pointer to the widget
    return self;
}

EXPORT void button_set_text(widget_t *self, char *text)
{
    // free the old text, alloc a new string and copy the given text
    free(self->text);
    self->text = malloc(strlen(text)+1);
    assert(self->text != NULL);
    strcpy(self->text, text);
    // send the text to dialog
    SendMessage(self->hwnd, WM_SETTEXT, 0, (WPARAM)self->text);
}

